<?php
namespace app\gr\controller;

use app\gr\BLL\CardRedeemBLL;
use cmf\controller\AdminBaseController;

class AdminCardRedeemController extends AdminBaseController
{
    private $bll;
    private $ControlName = "AdminCardRedeem";

    public function __construct(CardRedeemBLL $cardRedeem)
    {
        parent::__construct();
        $this->bll = $cardRedeem;
    }

    public function index()
    {
        $where = [];
        $request = input('request.');
        if (!empty($request['tpname'])) {
            $tpname = $request['tpname'];
            $where['type.name'] = ['like', "%$tpname%"];
        }
        if (!empty($request['name'])) {
            $name = $request['name'];
            $where['a.name'] = ['like', "%$name%"];
        }
        if (!empty($request['tele'])) {
            $tele = $request['tele'];
            $where['a.tele'] = ['like', "%$tele%"];
        }

        $res = $this->bll->getListPage($where);

        $this->assign('list', $res['list']);
        $this->assign('page', $res['page']);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $redeem = $this->bll->getbyid($id);
        $this->assign('redeem',$redeem);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', 0, 'intval');
            $data = $this->request->param();

            if($this->bll->update($data,$id)){
                $this->success("更新成功！", url("$this->ControlName/index"));
            }else{
                $this->error('更新失败！');
            }
        }
    }
}
